#include "ParserGameInfoChecker.h"

ParserGameInfoChecker::ParserGameInfoChecker(){
    _gameHeaders = new QMap<QString, bool>();
    (*_gameHeaders)["Event"]=false;
    (*_gameHeaders)["Site"]=false;
    (*_gameHeaders)["Date"]=false;
    (*_gameHeaders)["Round"]=false;
    (*_gameHeaders)["White"]=false;
    (*_gameHeaders)["Black"]=false;
    (*_gameHeaders)["Result"]=false;
}

int ParserGameInfoChecker::addHeader(QString name, QString value){
    if (value.size()==0)
        return -1;
    else if (!_gameHeaders->contains(name)||(_gameHeaders->contains(name)&&!(*_gameHeaders)[name])){
        if (name.compare("Site")==0){
            QRegExp rx("*[0-9]*");
            rx.setPatternSyntax(QRegExp::Wildcard);
            if (!value.contains(' ')&& value.compare("?")!=0){
                (*_gameHeaders)[name]=true;
                return 2;
            }
            if (rx.exactMatch(value))
                return 2;
        }else if (name.compare("Date")==0||name.compare("EventDate")==0){
            QRegExp data("[0-9|\\?]{4}.[0-9|\\?]{2}.[0-9|\\?]{2}");
            if (!data.exactMatch(value))
                return 3;
        }else if (name.compare("Round")==0){
            QRegExp runda("[0-9|\\?\\.]{1,}|[\\?|\\-]{1}");
            if (!runda.exactMatch(value))
                return 4;
        }else if (name.compare("Result")==0){
            QRegExp wynik("(1/2-1/2)|(0-1)|(1-0)|(\\*)");
            if (!wynik.exactMatch(value))
                return 5;
        }else if (name.compare("ECO")==0){
            QRegExp eco("[A-E]{1}[0-9]{2}");
            if (!eco.exactMatch(value))
                return 6;
        }else if (name.compare("WhiteElo")==0|| name.compare("BlackElo")==0||name.compare("WhiteUSCF")==0|| name.compare("BlackUSCF")==0){
            QRegExp elo("[0-9]{1,}|[\\-]{1}");
            if (!elo.exactMatch(value))
                return 7;
        }else if (name.compare("WhiteType")==0|| name.compare("BlackType")==0){
            QRegExp eco("[human|program]");
            if (!eco.exactMatch(value))
                return 8;
        }else{
            QString others = "Event,White,Black,WhiteTitle,BlackTitle,WhiteNA,BlackNA,EventSponsor,Section,Stage,Board,Opening,Variation,SubVariation,NIC,UTCTime,UTCDate,TimeControl,SetUp,FEN,Termination,Annotator,Mode,PlyCount,";
            if (!others.contains(name+','))
                return 9;
        }
        (*_gameHeaders)[name]=true;
        return 0;
    }else
        return 1;
}

bool ParserGameInfoChecker::checkCorrect(){
    QMap<QString, bool>::iterator i;
    for (i = _gameHeaders->begin(); i != _gameHeaders->end(); ++i){
        if (i.value()==false)
            return false;
    }
    return true;
}

ParserGameInfoChecker::~ParserGameInfoChecker(){
    delete _gameHeaders;
}
